package com.xuhai.blog.api;

import com.xuhai.blog.admin.entity.ArticleEntity;
import com.xuhai.blog.admin.entity.CommentEntity;
import com.xuhai.blog.admin.service.ArticleService;
import com.xuhai.blog.admin.service.CommentService;
import com.xuhai.blog.common.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2022/2/15/015
 */
@RestController
@RequestMapping("/api/article")
public class ApiArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    @GetMapping("/page/{page}")
    public ResultJson page(@PathVariable Integer page){
        ArticleEntity article=new ArticleEntity();
        article.setStatus(1);
        article.setArticleStatus(1);
        return ResultJson.ok().data(articleService.getPage(page,5,article));
    }
    @GetMapping("/hot/{page}")
    public ResultJson hot(@PathVariable Integer page){
        ArticleEntity article=new ArticleEntity();
        article.setStatus(1);
        article.setArticleStatus(1);
        article.setReadCount(1);
        return ResultJson.ok().data(articleService.getPage(page,5,article));
    }

    @GetMapping("/category/{categoryId}/{page}")
    public ResultJson pageByCategory(@PathVariable Integer categoryId,@PathVariable Integer page){
        ArticleEntity article=new ArticleEntity();
        article.setStatus(1);
        article.setArticleStatus(1);
        return ResultJson.ok().data(articleService.getPageByCategoryId(page,5,categoryId,article));
    }

    @GetMapping("/{articleId}")
    public ResultJson detail(@PathVariable Integer articleId){
        return ResultJson.ok().data(articleService.getById(articleId));
    }

    @PostMapping("/{articleId}/read")
    public ResultJson read(@PathVariable Integer articleId){
        articleService.updateRead(articleId);
        return ResultJson.ok();
    }

    @GetMapping("/{articleId}/comment/{page}")
    public ResultJson commentPage(@PathVariable Integer page,@PathVariable Integer articleId){
        CommentEntity comment = new CommentEntity();
        comment.setStatus(1);
        comment.setCommentStatus(2);
        comment.setArticleId(articleId);
        return ResultJson.ok().data(commentService.getPage(page,5,comment));
    }

}
